<script setup>
import { onMounted, ref } from 'vue'
import { getSupplierList, deleteSupplier } from '@/api/supplier'
import { ElMessage } from 'element-plus';
import addAndUpdateView from './addAndUpdate.vue'
const tableData = ref([])
const queryForm = ref({
  supplierName: '',
  linkPhone: '',
})
// 分页
const currentPage = ref(1)
const pageSize = ref(5)
const total = ref(0)
const handleSizeChange = (val) => {
  currentPage.value = 1
  pageSize.value = val
  getData()
}
const handleCurrentChange = (val) => {
  currentPage.value = val
  getData()
}

// 查询
const getData = async () => {
  const res = await getSupplierList(queryForm.value.supplierName, queryForm.value.linkPhone, currentPage.value, pageSize.value)
  // const res = await getSupplierList()
  tableData.value = res.data.rows
  total.value = res.data.total
}

// 清空
const cancle = () => {
  queryForm.value.supplierName = ''
  queryForm.value.linkPhone = ''
  getData()
}
const addAndUpdate = ref(null)
const add = () => {
  addAndUpdate.value.supplierForm = {}
  addAndUpdate.value.dialogFormVisible = true
  addAndUpdate.value.title = '添加供应商'
}
const handleClick = (row) => {
  addAndUpdate.value.supplierForm = { ...row }
  addAndUpdate.value.dialogFormVisible = true
  addAndUpdate.value.title = '修改供应商'
}
const deleteById = async (id) => {
  let res = await deleteSupplier(id)
  getData()
  if (res.code == 200) {
    ElMessage.success('操作成功')
    dialogFormVisible.value = false
  } else {
    ElMessage.error('操作失败')
  }
}
onMounted(() => {
  getData()
})
</script>
<template>
  <el-form :inline="true" :model="queryForm" label-width="100px">
    <el-form-item label="供应商名字：" style="width: 235px;">
      <el-input v-model="queryForm.supplierName" placeholder="请输入供应商名字:"></el-input>
    </el-form-item>
    <el-form-item label="联系人电话：" style="width: 235px;">
      <el-input v-model="queryForm.linkPhone" placeholder="请输入联系人电话:"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="getData">查询</el-button>
    </el-form-item>
    <el-form-item>
      <el-button type="default" @click="cancle">清空</el-button>
    </el-form-item>
    <el-form-item>
      <el-button type="success" @click="add"> + 新增供应商</el-button>
    </el-form-item>
  </el-form>

  <el-table border :data="tableData" style="width: 100%;text-align: center;" stripe="true">
    <el-table-column type="index" prop="id" label="编号" width="120" align="center" />
    <el-table-column prop="supplierName" label="供应商名字" width="180" align="center" />
    <el-table-column prop="linkName" label="联系人名字" width="180" align="center" />
    <el-table-column prop="linkPhone" label="联系人电话" width="180" align="center" />
    <el-table-column label="城市" width="180" align="center" prop="cityCode">
      <template #default="scope">
        <el-tag type="primary">{{ scope.row.cityName }}</el-tag>
      </template>
    </el-table-column>
    <el-table-column label="省份" width="180" align="center" prop="provinceId">
      <template #default="scope">
        <el-tag type="success">{{ scope.row.provinceName }}</el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="address" label="详细地址" width="180" align="center" />
    <!-- <el-table-column prop="saasId" label="SaaS标识" width="180" align="center" /> -->
    <el-table-column fixed="right" label="操作" width="120" align="center">
      <template #default="scope">
        <el-button link type="success" size="large" @click="handleClick(scope.row)"> 编辑 </el-button>
        <el-button link type="danger" size="large" @click="deleteById(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
  <br />
  <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[5, 10, 15, 20]"
    layout="total, sizes, prev, pager, next, jumper" :total="total" background=true @size-change="handleSizeChange"
    @current-change="handleCurrentChange" />

  <addAndUpdateView ref="addAndUpdate" @query="getData" />
</template>
<style scoped>
.el-table .warning-row {
  --el-table-tr-bg-color: var(--el-color-warning-light-9);
}

.el-table .success-row {
  --el-table-tr-bg-color: var(--el-color-success-light-9);
}
</style>